import 'package:dartx/dartx.dart';
import 'package:get/get.dart';
import 'package:gluz/app/data/user_vip_entity.dart';
import 'package:gluz/generated/json/base/json_field.dart';
import 'package:gluz/generated/json/user_profile_entity.g.dart';
import 'dart:convert';

import '../../app_session.dart';
import 'identity_entity.dart';

export 'package:gluz/generated/json/user_profile_entity.g.dart';


@JsonSerializable()
class TokenEntity {
	@JSONField(name: 'access_token')
	late String accessToken;
	@JSONField(name: 'expire_in')
	late int expireIn;

	TokenEntity();

	factory TokenEntity.fromJson(Map<String, dynamic> json) => $TokenEntityFromJson(json);

	Map<String, dynamic> toJson() => $TokenEntityToJson(this);

	@override
	String toString() {
		return jsonEncode(this);
	}
}

@JsonSerializable()
class UserProfileEntity {
	String id = '';
	String countryCode = '';
	String phoneNumber = '';
	String country = '';
	String nickName = '';
	int areaId = 0;
	String avatar = '';
	String identity = '';
	// 1：正常，2：锁定
	String status = '';
	// 0：未认证，1：认证成功，2：认证失败，3审核中
	String authStatus = '';
	//认证拒绝原因
	dynamic rejectReason;
	// 1：个人，2：企业
	String authType = '';
	String createTime = '';

	// '商城加好友是否展示 1:是 0:否',
	String productSwitch = '0';

	String get token {
		return app.getToken() ?? '';
	}

	bool get isChina {
		return country == '0';
	}

	// 会员等级
	UserVipEntity vipEntity = UserVipEntity();


	String authStatusString() {
		if (authStatus == '0') {
			return '未认证'.tr;
		} else if (authStatus == '1') {
			return '已认证'.tr;
		} else if (authStatus == '2') {
			return '认证失败'.tr;
		} else {
			return '审核中'.tr;
		}
	}

	IdentityEntity getIdentity() {
		int identityIndex = identity.toIntOrNull() ?? 6;
		return IdentityEntity.dataSource()[identityIndex];
	}

	/// 是否是货代/运输
	bool isHuodaiOrYunshu() {
		return getIdentity().id == 2 || getIdentity().id == 3;
	}

	UserProfileEntity();

	factory UserProfileEntity.fromJson(Map<String, dynamic> json) => $UserProfileEntityFromJson(json);

	Map<String, dynamic> toJson() => $UserProfileEntityToJson(this);

	@override
	String toString() {
		return jsonEncode(this);
	}
}